package com.monkeybrother.utils;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;

public class CRC16Util {

    private static final int POLYNOMIAL = 0xA001;

    public static int calculateCRC16(byte[] data) {
        int crc = 0xFFFF;
        for (byte b : data) {
            crc ^= (b & 0xff);
            for (int i = 0; i < 8; i++) {
                if ((crc & 1) == 1) {
                    crc >>= 1;
                    crc ^= POLYNOMIAL;
                } else {
                    crc >>= 1;
                }
            }
        }
        return crc;
    }

    public static void main(String[] args) throws Exception {



        FileInputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\rtthread.bin"));
        int len = in.available();
        byte[] fileData = new byte[in.available()];
        in.read(fileData);
        in.close();
        int i = (int) Math.ceil(len / 1024.0);

        System.out.println("分包:" + Math.ceil(len / 1024.0));
        System.out.println(Arrays.toString(fileData));
        int l = len - 122*1024;
        byte[] b = new byte[l];
        System.arraycopy(fileData, 124928, b, 0 ,l);
        System.out.println(Arrays.toString(b));
        System.out.println("---");

    }


    public static String Byte2Hex(byte[] inByte) {

        StringBuilder sb = new StringBuilder();
        String hexString;

        for (int i = 0; i < inByte.length; i++) {

            //toHexString方法用于将16进制参数转换成无符号整数值的字符串
            //与0xFF做与运算，消除byte符号位为负数带来的影响
            String hex = Integer.toHexString(0xFF & inByte[i]);

            if (hex.length() == 1) {
                sb.append("0");//当16进制为个位数时，在前面补0
            }
            sb.append(hex);//将16进制加入字符串

        }

        hexString = sb.toString();
        hexString = hexString.toUpperCase();//将16进制字符串中的字母大写

        return hexString;

    }

    private static byte[] a(int i) {

        byte b[] = new byte[2];
        b[0] = (byte) ((i & 0xff00) >> 8);
        b[1] = (byte) (i & 0xff);

        String format = String.format("%02x", b[0]);
        String format1 = String.format("%02x", b[1]);
        byte r[] = new byte[2];
        r[0] = b[1];
        r[1] = b[0];

        return r;
    }

    public static String decimalToHex(int decimal) {
        StringBuilder hex = new StringBuilder();
        while (decimal != 0) {
            int remainder = decimal % 16;
            if (remainder < 10) {
                hex.insert(0, remainder);
            } else {
                hex.insert(0, (char) (remainder + 55));
            }
            decimal /= 16;
        }
        return hex.toString();
    }
    public static byte[] bytesMerge(byte[] byte1, byte[] byte2) {
        byte[] byte3 = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, byte3, 0, byte1.length);
        System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length);
        return byte3;
    }

    public static byte[] pkgBytes(int num) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (num >> 8);
        bytes[1] = (byte) ((num & 0xff));
        return bytes;
    }


    public static byte[] crcBytes(byte[] data) {
        int crc = 0xFFFF;
        for (byte b : data) {
            crc ^= (b & 0xff);
            for (int i = 0; i < 8; i++) {
                if ((crc & 1) == 1) {
                    crc >>= 1;
                    crc ^= 0xA001;
                } else {
                    crc >>= 1;
                }
            }
        }
        byte[] bytes = new byte[2];
        bytes[0] = (byte) ((crc & 0xff));
        bytes[1] = (byte) (crc >> 8);
        return bytes;
    }

}